package uiLayer.uiChartecCode;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import ctrLayer.CtrChartecCode;
import uiLayer.uiUtilities.StandardDialog;

import java.awt.Component;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ChartecCodeCard1 extends JPanel {

	private JPanel chartecPanel;
	private JPanel buttonPanel;
	private JButton btnSave;
	private JButton btnCancel;
	private JTextField txtCcode;
	private JTextField txtShippingPlace;
	private JCheckBox checkDelNo;
	private StandardDialog dialog;
	private CtrChartecCode ctrCc;


	/**
	 * Create the panel.
	 */
	public ChartecCodeCard1() {
		dialog = new StandardDialog();
		ctrCc = new CtrChartecCode();
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{10, 300, 10};
		gridBagLayout.rowHeights = new int[]{10, 150, 30, 10};
		gridBagLayout.columnWeights = new double[]{1.0, 0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);

		chartecPanel = chartecPanel();
		add(chartecPanel, gbc_field(1, 1));

		buttonPanel = buttonPanel();
		add(buttonPanel, gbc_field(1, 2));

	}

	/**
	 * Method that creates and returns the panel for driver data.
	 * 
	 * @return
	 */
	private JPanel chartecPanel() {
		chartecPanel = new JPanel();
		chartecPanel.setBorder(new TitledBorder(null, "Chartec kode", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GridBagLayout gbl_driverPanel = new GridBagLayout();
		gbl_driverPanel.columnWidths = new int[]{10, 110, 150, 10};
		gbl_driverPanel.rowHeights = new int[]{10, 30, 30, 30, 10};
		gbl_driverPanel.columnWeights = new double[]{1.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		gbl_driverPanel.rowWeights = new double[]{1.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		chartecPanel.setLayout(gbl_driverPanel);

		//Code
		JLabel lblCcode = new JLabel("Kode (4 cifre):");
		chartecPanel.add(lblCcode, gbc_lbl(1, 1));

		txtCcode = new JTextField();
		txtCcode.getDocument().addDocumentListener(new DocumentListener() {

			@Override
			public void changedUpdate(DocumentEvent arg0) {
				checkInput(txtCcode);
			}

			@Override
			public void insertUpdate(DocumentEvent arg0) {
				checkInput(txtCcode);
			}

			@Override
			public void removeUpdate(DocumentEvent arg0) {
				checkInput(txtCcode);
			}

		});
		chartecPanel.add(txtCcode, gbc_field(2, 1));

		//Shipping place
		JLabel lblShippingPlace = new JLabel("Via sted:");
		chartecPanel.add(lblShippingPlace, gbc_lbl(1, 2));

		txtShippingPlace = new JTextField();
		chartecPanel.add(txtShippingPlace, gbc_field(2, 2));

		//Has deliveryNo
		JLabel lblDelNo = new JLabel("Indleverings nr.:");
		chartecPanel.add(lblDelNo, gbc_lbl(1, 3));

		checkDelNo = new JCheckBox();
		chartecPanel.add(checkDelNo, gbc_field(2, 3));

		return chartecPanel;
	}

	/**
	 * Method that creates and returns the panel with buttons.
	 * 
	 * @return
	 */
	private JPanel buttonPanel() {
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));

		Component horizontalGlue = Box.createHorizontalGlue();
		buttonPanel.add(horizontalGlue);

		btnSave = new JButton("Gem");
		btnSave.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				saveChartecCode();
			}

		});
		buttonPanel.add(btnSave);

		Component horizontalStrut = Box.createHorizontalStrut(10);
		buttonPanel.add(horizontalStrut);

		btnCancel = new JButton("Annuller");
		btnCancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				clearFields();
			}

		});
		buttonPanel.add(btnCancel);

		return buttonPanel;
	}

	protected void checkInput(final JTextField txt) {
		Runnable checkForChars = new Runnable() {
			@Override
			public void run() {
				if(txt.getText().length() > 0) {
					try{
						Integer.parseInt(txt.getText());
					} catch (Exception e) {
						txt.setText(txt.getText().substring(0, txt.getText().length()-1));
					}
				}

				if(txt.getText().length() > 4) {
					txt.setText(txt.getText().substring(0, 4));
				}
			}
		};
		SwingUtilities.invokeLater(checkForChars);

	}

	protected void saveChartecCode() {
		if(requiredInput()) {
			try {
				ctrCc.create(Integer.parseInt(txtCcode.getText()), txtShippingPlace.getText(), checkDelNo.isSelected());
				dialog.objectSaved("Chartec Code");
				clearFields();
			} catch(Exception e) {
				e.printStackTrace();
				dialog.errorOnCreate();
			}
		}
	}

	private boolean requiredInput() {
		boolean ok = false;
		if(txtCcode.getText().length() == 0) {
			dialog.requiresInput("Kode (4 cifre)");
		} else if(txtShippingPlace.getText().length() == 0) {
			dialog.requiresInput("Via sted");
		} else {
			ok = true;
		}
		return ok;
	}

	protected void clearFields() {
		txtCcode.setText("");
		txtShippingPlace.setText("");
		checkDelNo.setSelected(false);
	}

	private GridBagConstraints gbc_field(int x, int y) {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(0, 0, 5, 5);
		gbc.fill = GridBagConstraints.BOTH;
		gbc.gridx = x;
		gbc.gridy = y;
		return gbc;
	}

	private GridBagConstraints gbc_lbl(int x, int y) {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.WEST;
		gbc.insets = new Insets(0, 0, 5, 5);
		gbc.gridx = x;
		gbc.gridy = y;
		return gbc;
	}

}
